<?php

declare (strict_types=1);
namespace App\Model;

use App\Traits\ModelLabelTrait;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $paper_id 
 * @property int $admin_id 
 * @property string $title 
 * @property string $image 
 * @property int $type 
 * @property string $explain 
 * @property string $answers 
 * @property string $right_result 
 * @property int $score 
 * @property int $listorder 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $deleted_at 
 */
class PaperQuestion extends Model
{
    use SoftDeletes,ModelLabelTrait;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'paper_questions';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'paper_id',
        'listorder',
        'image',
        'explain',
        'type',
        'admin_id',
        'score'
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'paper_id' => 'integer', 'admin_id' => 'integer', 'type' => 'integer', 'score' => 'integer', 'listorder' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    protected $appends=[
        'type_label'
    ];

    const TYPE_Judgment =1;
    const TYPE_RADIO = 2;
    const TYPE_CHECKBOX = 3;
    const TYPE_INPUT = 4;

    const TYPE_LABELS=[
        self::TYPE_Judgment => '判断题',
        self::TYPE_RADIO => '单选题',
        self::TYPE_CHECKBOX => '多选题',
        self::TYPE_INPUT => '填空题'
    ];

    public function getTypeLabelAttribute(){
        return self::getLabel('type',$this->type);
    }

    public function getImageAttribute($field)
    {
        return $this->attributes[$field] = $field?config('common.image_prefix').$field:null;
    }

    public function getAnswersAttribute($field)
    {
        return $this->attributes[$field] = $field?json_decode($field,true):null;
    }

    public function setImageAttribute($value)
    {
        return $this->attributes['image'] = str_replace(config('common.image_prefix'),'',$value);
    }
}